package ru.dadle8.service.impl.action.workout;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import ru.dadle8.model.bot.entity.State;
import ru.dadle8.model.bot.type.Action;
import ru.dadle8.model.bot.type.CommandState;
import ru.dadle8.model.db.Workout;
import ru.dadle8.service.impl.action.AbstractActionService;

import java.util.stream.Collectors;

@Service
public class GetAllWorkoutsCommandServiceImpl extends AbstractActionService {

    @Override
    public String getAction() {
        return Action.GET_WORKOUTS.getAction();
    }

    @Override
    public void processUpdate(Update update) throws TelegramApiException {
        Page<Workout> workouts = getWorkoutService().findAllWorkouts(0);

        InlineKeyboardMarkup inlineKeyboardMarkup = getKeyboardService().getInlineKeyboardMarkupWithPagination(
                workouts.stream().map(Workout::getTitle).collect(Collectors.toList()),
                workouts.stream().map(Workout::getMessageId).collect(Collectors.toList()),
                1);

        getStorage().updateState(new State(0, CommandState.GET_WORKOUT, getChatId()));
        getBot().execute(getMessageHelper().sendMessage(getChatId(), "Выбери тренировку. Страница №%s".formatted(1), inlineKeyboardMarkup));
    }

}
